package com.zd.miko.tally.MVP.ModuleEditor;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zd.miko.tally.CustomView.RichEditText.RichTextEditor;
import com.zd.miko.tally.MyApp;
import com.zd.miko.tally.R;

import java.io.File;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by Miko on 2017/7/18.
 */

public class ImagePreviewActivity extends AppCompatActivity {

    ImageView jpegView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_image_preview);

        jpegView = (ImageView) findViewById(R.id.id_center_image);

        findViewById(R.id.id_rl_back).setOnClickListener(v -> finish());

        String path = getIntent().getStringExtra("path");
        int imageType = getIntent().getIntExtra("type", 0);

        if (imageType == RichTextEditor.IMAGE) {
            if (path != null) {

                if(path.contains("http")){
                    Glide.with(MyApp.context)
                            .load(path).into(jpegView);
                    return;
                }

                String srcPath = path;

                String sufStr = srcPath.split("meiriji")[1];

                String netPath = MyApp.context
                        .getString(R.string.host) + "img" + sufStr;

                if (!new File(srcPath).exists()) {
                    Glide.with(MyApp.context)
                            .load(netPath).into(jpegView);
                } else {
                    Glide.with(MyApp.context)
                            .load(srcPath).into(jpegView);
                }
            }
        }else if(imageType == RichTextEditor.GIF){
            jpegView.setVisibility(View.VISIBLE);


            if(path.contains("http")){
                Glide.with(MyApp.context)
                        .load(path).into(jpegView);
                return;
            }

            String srcPath = path;

            String sufStr = srcPath.split("meiriji")[1];

            String netPath = MyApp.context
                    .getString(R.string.host) + "img" + sufStr;

            if (!new File(srcPath).exists()) {
                Glide.with(MyApp.context)
                        .load(netPath).into(jpegView);
            } else {
                Glide.with(MyApp.context)
                        .load(srcPath).into(jpegView);
            }

        }else if(imageType == RichTextEditor.VIDEO){


            jpegView.setVisibility(View.GONE);
            JCVideoPlayerStandard jcVideoPlayerStandard =
                    (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
            jcVideoPlayerStandard.backButton.setVisibility(View.GONE);
            jcVideoPlayerStandard.setVisibility(View.VISIBLE);

            if(path.contains("http")){
                jcVideoPlayerStandard.setUp(path
                        , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL);
                Glide.with(MyApp.context)
                        .load(path).into(jcVideoPlayerStandard.thumbImageView);

                return;
            }


            String srcPath = path;

            String sufStr = srcPath.split("meiriji")[1];

            String netPath = MyApp.context
                    .getString(R.string.host) + "img" + sufStr;

            if(new File(srcPath).exists()){

                jcVideoPlayerStandard.setUp(srcPath
                        , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL);
                Glide.with(MyApp.context)
                        .load(srcPath).into(jcVideoPlayerStandard.thumbImageView);

            }else{
                jcVideoPlayerStandard.setUp(netPath
                        , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL);
                Glide.with(MyApp.context)
                        .load(netPath).into(jcVideoPlayerStandard.thumbImageView);
            }

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    public static Bitmap getVideoThumbnail(String videoPath) {
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(videoPath);
        Bitmap bitmap = media.getFrameAtTime();
        return bitmap;
    }
}
